package com.itheima.system.controller;

import com.itheima.common.entity.Result;
import com.itheima.common.entity.ResultCode;
import com.itheima.system.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * @author IntelliJ IDEA
 * @date 2020/10/26  15:42
 *
 * @CrossOrigin： 解决ajax跨域
 */
@CrossOrigin
@RequestMapping("/sys")
@RestController
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 新增权限
     */
    @PostMapping("/permission")
    public Result addPermission(@RequestBody Map<String,Object> map){
        try{
            permissionService.addPermission(map);
            return new Result(ResultCode.SUCCESS);
        }catch (Exception e){
            return new Result(ResultCode.FAIL);
        }
    }

    /**
     * 修改权限
     */
    @PutMapping("/permission/{id}")
    public Result updatePermission(@PathVariable("id") String id,@RequestBody Map<String,Object> map){
        map.put("id",id);
        try{
            permissionService.updatePermission(map);
            return new Result(ResultCode.SUCCESS);
        }catch (Exception e){
            return new Result(ResultCode.FAIL);
        }
    }


    /**
     * 查询列表
     */
    @GetMapping("/permission")
    public Result findAllPermission(@RequestParam Map<String,Object> map){
        try{
            return new Result(ResultCode.SUCCESS,permissionService.findAllPermission(map));
        }catch (Exception e){
            return new Result(ResultCode.FAIL);
        }
    }


    /**
     * 根据id查询
     */
    @GetMapping("/permission/{id}")
    public Result findPermissionById(@RequestParam("id") String id){
        try{
            System.out.println("id = " + id);
            return new Result(ResultCode.SUCCESS,permissionService.findPermissionById(id));
        }catch (Exception e){
            return new Result(ResultCode.FAIL);
        }
    }


    /**
     * 删除权限
     */
    @DeleteMapping("/permission/{id}")
    public Result deletePermission(@PathVariable("id") String id){
        try{
            permissionService.deletePermission(id);
            return new Result(ResultCode.SUCCESS);
        }catch (Exception e){
            return new Result(ResultCode.FAIL);
        }
    }

}
